package lib.bone.flow.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lib.bone.flow.dao.FlowDefineDAO;
import lib.bone.flow.domain.dto.FlowDefineAddDTO;
import lib.bone.flow.domain.entity.FlowDefine;
import lib.bone.flow.service.FlowDefineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author: kindear
 * @date : 2022-2-15
 */
@Slf4j
@Service
public class FlowDefineServiceImpl implements FlowDefineService {

    @Autowired
    FlowDefineDAO flowDefineDAO;

    @Override
    public ResponseDTO deleteById(String id) {
        flowDefineDAO.deleteById(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @Override
    public Optional<FlowDefine> getByDefineCode(String defineCode) {
        return flowDefineDAO.findByFlowDefineCode(defineCode);
    }

    @Override
    public Optional<FlowDefine> getById(String id) {
        return flowDefineDAO.findById(id);
    }

    @Override
    public FlowDefine saveOne(FlowDefine flowDefine) {
        if (Validator.isNotEmpty(flowDefine.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return flowDefineDAO.save(flowDefine);
    }

    @Override
    public FlowDefine updateOne(FlowDefine flowDefine) {
        if (Validator.isEmpty(flowDefine.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return flowDefineDAO.save(flowDefine);
    }

    @Override
    public PageResultDTO<FlowDefine> getAll(Pageable pageable, Example<FlowDefine> example) {
        Page<FlowDefine> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = flowDefineDAO.findAll(pageable);
        }else {
            page = flowDefineDAO.findAll(example,pageable);
        }
        List<FlowDefine> list = page.getContent();
        PageResultDTO<FlowDefine> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());
        return pageResultDTO;
    }

//    @Override
//    public Optional<FlowDefine> getOneById(String id) {
//        return flowDefineDAO.findById(id);
//    }
//
//    @Override
//    public Optional<FlowDefine> getOneByFlowDefineCode(String flowDefineCode) {
//        return flowDefineDAO.findByFlowDefineCode(flowDefineCode);
//    }
//
//    @Override
//    public ResponseDTO<FlowDefine> saveOne(FlowDefineAddDTO dto) {
//
//        FlowDefine define = new FlowDefine();
//        /**
//         * 流程定义编码不可重复
//         */
//        Optional<FlowDefine> defineOp = getOneByFlowDefineCode(dto.getFlowDefineCode());
//        if(defineOp.isPresent()){
//            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM,"DefineCode重复");
//        }
//        /**
//         * 不重复则写入
//         */
//        BeanUtil.copyProperties(dto,define, CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
//        return ResponseDTO.succData(flowDefineDAO.save(define));
//    }
}
